Merge pull request #468 from dsander/job-management

Simple job management page

Dominik Sander 10 years ago
parent
commit
3fa68bd1b3

+ 4 - 1
.env.example

@@ -124,4 +124,7 @@ ENABLE_INSECURE_AGENTS=false
124 124
 #USE_GRAPHVIZ_DOT=dot
125 125
 
126 126
 # Timezone. Use `rake time:zones:local` or `rake time:zones:all` to get your zone name
127
-TIMEZONE="Pacific Time (US & Canada)"
127
+TIMEZONE="Pacific Time (US & Canada)"
128
+
129
+# Number of failed jobs to keep in the database
130
+FAILED_JOBS_TO_KEEP=100

+ 0 - 3
Gemfile

@@ -45,9 +45,6 @@ gem 'delayed_job', '~> 4.0.0'
45 45
 gem 'delayed_job_active_record', '~> 4.0.0'
46 46
 gem 'daemons', '~> 1.1.9'
47 47
 
48
-# To enable DelayedJobWeb, see the 'Enable DelayedJobWeb' section of the README.
49
-# gem 'delayed_job_web'
50
-
51 48
 gem 'foreman', '~> 0.63.0'
52 49
 
53 50
 gem 'sass-rails',   '~> 4.0.0'

+ 0 - 6
README.md

@@ -85,12 +85,6 @@ See [private development instructions](https://github.com/cantino/huginn/wiki/Pr
85 85
 
86 86
 In order to use the WeatherAgent you need an [API key with Wunderground](http://www.wunderground.com/weather/api/). Signup for one and then change the value of `api_key: your-key` in your seeded WeatherAgent.
87 87
 
88
-#### Enable DelayedJobWeb for handy delayed\_job monitoring and control
89
-
90
-* Edit `config.ru`, uncomment the DelayedJobWeb section, and change the DelayedJobWeb username and password.
91
-* Uncomment `match "/delayed_job" => DelayedJobWeb, :anchor => false` in `config/routes.rb`.
92
-* Uncomment `gem "delayed_job_web"` in Gemfile and run `bundle`.
93
-
94 88
 #### Disable SSL
95 89
 
96 90
 We assume your deployment will run over SSL. This is a very good idea! However, if you wish to turn this off, you'll probably need to edit `config/initializers/devise.rb` and modify the line containing `config.rememberable_options = { :secure => true }`.  You will also need to edit `config/environments/production.rb` and modify the value of `config.force_ssl`.

+ 26 - 13
app/assets/javascripts/worker-checker.js.coffee

@@ -1,22 +1,29 @@
1 1
 $ ->
2 2
   firstEventCount = null
3
+  previousJobs = null
3 4
 
4
-  if $("#job-indicator").length
5
+  if $(".job-indicator").length
5 6
     check = ->
6 7
       $.getJSON "/worker_status", (json) ->
7
-        firstEventCount = json.event_count unless firstEventCount?
8
-
9
-        if json.pending? && json.pending > 0
10
-          tooltipOptions = {
11
-            title: "#{json.pending} jobs pending, #{json.awaiting_retry} awaiting retry, and #{json.recent_failures} recent failures"
12
-            delay: 0
13
-            placement: "bottom"
14
-            trigger: "hover"
15
-          }
16
-          $("#job-indicator").tooltip('destroy').tooltip(tooltipOptions).fadeIn().find(".number").text(json.pending)
17
-        else
18
-          $("#job-indicator:visible").tooltip('destroy').fadeOut()
8
+        for method in ['pending', 'awaiting_retry', 'recent_failures']
9
+          count = json[method]
10
+          elem = $(".job-indicator[role=#{method}]")
11
+          if count > 0
12
+            tooltipOptions = {
13
+              title: "#{count} jobs #{method.split('_').join(' ')}"
14
+              delay: 0
15
+              placement: "bottom"
16
+              trigger: "hover"
17
+            }
18
+            if elem.is(":visible")
19
+              elem.tooltip('destroy').tooltip(tooltipOptions).find(".number").text(count)
20
+            else
21
+              elem.tooltip('destroy').tooltip(tooltipOptions).fadeIn().find(".number").text(count)
22
+          else
23
+            if elem.is(":visible")
24
+              elem.tooltip('destroy').fadeOut()
19 25
 
26
+        firstEventCount = json.event_count unless firstEventCount?
20 27
         if firstEventCount? && json.event_count > firstEventCount
21 28
           $("#event-indicator").tooltip('destroy').
22 29
                                 tooltip(title: "Click to reload", delay: 0, placement: "bottom", trigger: "hover").
@@ -26,6 +33,12 @@ $ ->
26 33
         else
27 34
           $("#event-indicator").tooltip('destroy').fadeOut()
28 35
 
36
+        currentJobs = [json.pending, json.awaiting_retry, json.recent_failures]
37
+        if document.location.pathname == '/jobs' && $(".modal[aria-hidden=false]").length == 0 && previousJobs? && previousJobs.join(',') != currentJobs.join(',')
38
+          $.get '/jobs', (data) =>
39
+            $("#main-content").html(data)
40
+        previousJobs = currentJobs
41
+
29 42
         window.workerCheckTimeout = setTimeout check, 2000
30 43
 
31 44
     check()

+ 4 - 3
app/assets/stylesheets/application.css.scss.erb

@@ -88,9 +88,10 @@ span.not-applicable:after {
88 88
 }
89 89
 
90 90
 // Navbar
91
-
92
-#job-indicator, #event-indicator {
93
-  display: none;
91
+.nav > li {
92
+  &.job-indicator, &#event-indicator {
93
+    display: none;
94
+  }
94 95
 }
95 96
 
96 97
 .navbar-search > .spinner {

+ 3 - 0
app/assets/stylesheets/jobs.css.scss

@@ -0,0 +1,3 @@
1
+.big-modal-dialog {
2
+  width: 90% !important;
3
+}

+ 4 - 0
app/controllers/application_controller.rb

@@ -14,6 +14,10 @@ class ApplicationController < ActionController::Base
14 14
     devise_parameter_sanitizer.for(:account_update) { |u| u.permit(:username, :email, :password, :password_confirmation, :current_password) }
15 15
   end
16 16
 
17
+  def authenticate_admin!
18
+    redirect_to(root_path, alert: 'Admin access required to view that page.') unless current_user && current_user.admin
19
+  end
20
+
17 21
   def upgrade_warning
18 22
     return unless current_user
19 23
     twitter_oauth_check

+ 55 - 0
app/controllers/jobs_controller.rb

@@ -0,0 +1,55 @@
1
+class JobsController < ApplicationController
2
+  before_filter :authenticate_admin!
3
+
4
+  def index
5
+    @jobs = Delayed::Job.order("coalesce(failed_at,'1000-01-01'), run_at asc").page(params[:page])
6
+
7
+    respond_to do |format|
8
+      format.html { render layout: !request.xhr? }
9
+      format.json { render json: @jobs }
10
+    end
11
+  end
12
+
13
+  def destroy
14
+    @job = Delayed::Job.find(params[:id])
15
+
16
+    respond_to do |format|
17
+      if !running? && @job.destroy
18
+        format.html { redirect_to jobs_path, notice: "Job deleted." }
19
+        format.json { render json: "", status: :ok }
20
+      else
21
+        format.html { redirect_to jobs_path, alert: 'Can not delete a running job.' }
22
+        format.json { render json: "", status: :unprocessable_entity }
23
+      end
24
+    end
25
+  end
26
+
27
+  def run
28
+    @job = Delayed::Job.find(params[:id])
29
+    @job.last_error = nil
30
+
31
+    respond_to do |format|
32
+      if !running? && @job.update_attributes!(run_at: Time.now, failed_at: nil)
33
+        format.html { redirect_to jobs_path, notice: "Job enqueued." }
34
+        format.json { render json: @job, status: :ok }
35
+      else
36
+        format.html { redirect_to jobs_path, alert: 'Can not enqueue a running job.' }
37
+        format.json { render json: "", status: :unprocessable_entity }
38
+      end
39
+    end
40
+  end
41
+
42
+  def destroy_failed
43
+    Delayed::Job.where.not(failed_at: nil).delete_all
44
+
45
+    respond_to do |format|
46
+      format.html { redirect_to jobs_path, notice: "Failed jobs removed." }
47
+      format.json { render json: '', status: :ok }
48
+    end
49
+  end
50
+
51
+  private
52
+  def running?
53
+    (@job.locked_at || @job.locked_by) && @job.failed_at.nil?
54
+  end
55
+end

+ 4 - 0
app/helpers/application_helper.rb

@@ -38,4 +38,8 @@ module ApplicationHelper
38 38
       link_to 'No', agent_path(agent, tab: (agent.recent_error_logs? ? 'logs' : 'details')), class: 'label label-danger'
39 39
     end
40 40
   end
41
+
42
+  def user_is_admin?
43
+    current_user && current_user.admin == true
44
+  end
41 45
 end

+ 21 - 0
app/helpers/jobs_helper.rb

@@ -0,0 +1,21 @@
1
+module JobsHelper
2
+
3
+  def status(job)
4
+    case
5
+    when job.failed_at
6
+      content_tag :span, 'failed', class: 'label label-danger'
7
+    when job.locked_at && job.locked_by
8
+      content_tag :span, 'running', class: 'label label-info'
9
+    else
10
+      content_tag :span, 'queued', class: 'label label-warning'
11
+    end
12
+  end
13
+
14
+  def relative_distance_of_time_in_words(time)
15
+    if time < (now = Time.now)
16
+      time_ago_in_words(time) + ' ago'
17
+    else
18
+      'in ' + distance_of_time_in_words(time, now)
19
+    end
20
+  end
21
+end

+ 75 - 0
app/views/jobs/index.html.erb

@@ -0,0 +1,75 @@
1
+<div class='container'>
2
+  <div class='row'>
3
+    <div class='col-md-12'>
4
+      <div class="page-header">
5
+        <h2>
6
+          Background Jobs
7
+        </h2>
8
+      </div>
9
+
10
+      <div class='table-responsive'>
11
+        <table class='table table-striped events'>
12
+          <tr>
13
+            <th>Status</th>
14
+            <th>Created</th>
15
+            <th>Next Run</th>
16
+            <th>Attempts</th>
17
+            <th>Last Error</th>
18
+            <th></th>
19
+          </tr>
20
+
21
+        <% @jobs.each do |job| %>
22
+          <tr>
23
+            <td><%= status(job) %></td>
24
+            <td title='<%= job.created_at %>'><%= time_ago_in_words job.created_at %> ago</td>
25
+            <td title='<%= job.run_at %>'>
26
+              <% if !job.failed_at %>
27
+                <%= relative_distance_of_time_in_words job.run_at %>
28
+              <% end %>
29
+            </td>
30
+            <td><%= job.attempts %></td>
31
+            <td>
32
+              <a data-toggle="modal" data-target="#error<%= job.id %>"><%= truncate job.last_error, :length => 90, :omission => "", :separator => "\n" %></a>
33
+              <div class="modal fade" id="error<%= job.id %>" tabindex="-1" role="dialog" aria-labelledby="#<%= "error#{job.id}" %>" aria-hidden="true">
34
+                <div class="modal-dialog big-modal-dialog">
35
+                  <div class="modal-content">
36
+                    <div class="modal-header">
37
+                      <button type="button" class="close" data-dismiss="modal"><span aria-hidden="true">&times;</span><span class="sr-only">Close</span></button>
38
+                      <h4 class="modal-title" id="myModalLabel">Error Backtrace</h4>
39
+                    </div>
40
+                    <div class="modal-body">
41
+                      <pre>
42
+                        <%= raw html_escape(job.last_error).split("\n").join('<br/>') %>
43
+                      </pre>
44
+                    </div>
45
+                  </div>
46
+                </div>
47
+              </div>
48
+            </td>
49
+            <td>
50
+              <% if (!job.locked_at && !job.locked_by) || job.failed_at.present? %>
51
+                <div class="btn-group btn-group-xs" style="float: right">
52
+                  <% if (job.run_at > Time.now) || job.failed_at.present? %>
53
+                    <%= link_to 'Run now', run_job_path(job), class: "btn btn-default", method: :put %>
54
+                  <% end %>
55
+                  <%= link_to 'Delete', job_path(job), class: "btn btn-danger", method: :delete, data: { confirm: 'Really delete this job?' } %>
56
+                </div>
57
+              <% end %>
58
+            </td>
59
+          </tr>
60
+        <% end %>
61
+        </table>
62
+      </div>
63
+
64
+      <%= paginate @jobs, :theme => 'twitter-bootstrap-3' %>
65
+
66
+      <br />
67
+      <div class="btn-group">
68
+        <%= link_to destroy_failed_jobs_path, class: "btn btn-default", method: :delete do %>
69
+          <span class="glyphicon glyphicon-trash"></span> Remove failed jobs
70
+        <% end %>
71
+      </div>
72
+    </div>
73
+  </div>
74
+</div>
75
+

+ 18 - 10
app/views/layouts/_navigation.html.erb

@@ -35,15 +35,19 @@
35 35
         </div>
36 36
       </form>
37 37
       
38
-      <li id='job-indicator'>
39
-        <% if defined?(DelayedJobWeb) %>
40
-          <a href="/delayed_job">
41
-            <span class="badge"><span class="glyphicon glyphicon-refresh icon-white"></span> <span class='number'>0</span></span>
42
-          </a>
43
-        <% else %>
44
-          <a href="#" onclick='return false;'>
45
-            <span class="badge"><span class="glyphicon glyphicon-refresh icon-white"></span> <span class='number'>0</span></span>
46
-          </a>
38
+      <li class='job-indicator' role='pending'>
39
+        <%= link_to current_user.admin? ? jobs_path : '#' do %>
40
+          <span class="badge"><span class="glyphicon glyphicon-refresh icon-white"></span> <span class='number'>0</span></span>
41
+        <% end %>
42
+      </li>
43
+      <li class='job-indicator' role='awaiting_retry'>
44
+        <%= link_to current_user.admin? ? jobs_path : '#' do %>
45
+          <span class="badge"><span class="glyphicon glyphicon-question-sign icon-yellow"></span> <span class='number'>0</span></span>
46
+        <% end %>
47
+      </li>
48
+      <li class='job-indicator' role='recent_failures'>
49
+        <%= link_to current_user.admin? ? jobs_path : '#' do %>
50
+          <span class="badge"><span class="glyphicon glyphicon-exclamation-sign icon-white"></span> <span class='number'>0</span></span>
47 51
         <% end %>
48 52
       </li>
49 53
       <li id='event-indicator'>
@@ -66,7 +70,11 @@
66 70
             <%= link_to 'Sign up', new_user_registration_path, :tabindex => "-1" %>
67 71
           <% end %>
68 72
         </li>
69
-
73
+        <% if user_signed_in? && current_user.admin %>
74
+          <li>
75
+            <%= link_to 'Job Management', jobs_path, :tabindex => '-1' %>
76
+          </li>
77
+        <% end %>
70 78
         <li>
71 79
           <%= link_to 'About', 'https://github.com/cantino/huginn', :tabindex => "-1" %>
72 80
         </li>

+ 3 - 1
app/views/layouts/application.html.erb

@@ -28,7 +28,9 @@
28 28
         <%= render "upgrade_warning" %>
29 29
       <% end %>
30 30
 
31
-      <%= yield %>
31
+      <div id="main-content">
32
+        <%= yield %>
33
+      </div>
32 34
       
33 35
     </div>
34 36
 

+ 0 - 8
config.ru

@@ -2,12 +2,4 @@
2 2
 
3 3
 require ::File.expand_path('../config/environment',  __FILE__)
4 4
 
5
-# To enable DelayedJobWeb, see the 'Enable DelayedJobWeb' section of the README.
6
-
7
-# if Rails.env.production?
8
-#  DelayedJobWeb.use Rack::Auth::Basic do |username, password|
9
-#    username == 'admin' && password == 'password'
10
-#  end
11
-# end
12
-
13 5
 run Huginn::Application

+ 1 - 1
config/initializers/delayed_job.rb

@@ -1,4 +1,4 @@
1
-Delayed::Worker.destroy_failed_jobs = true
1
+Delayed::Worker.destroy_failed_jobs = false
2 2
 Delayed::Worker.max_attempts = 5
3 3
 Delayed::Worker.max_run_time = 20.minutes
4 4
 Delayed::Worker.read_ahead = 5

+ 9 - 3
config/routes.rb

@@ -51,6 +51,15 @@ Huginn::Application.routes.draw do
51 51
     end
52 52
   end
53 53
 
54
+  resources :jobs, :only => [:index, :destroy] do
55
+    member do
56
+      put :run
57
+    end
58
+    collection do
59
+      delete :destroy_failed
60
+    end
61
+  end
62
+
54 63
   get "/worker_status" => "worker_status#show"
55 64
 
56 65
   post "/users/:user_id/update_location/:secret" => "user_location_updates#create"
@@ -58,9 +67,6 @@ Huginn::Application.routes.draw do
58 67
   match  "/users/:user_id/web_requests/:agent_id/:secret" => "web_requests#handle_request", :as => :web_requests, :via => [:get, :post, :put, :delete]
59 68
   post "/users/:user_id/webhooks/:agent_id/:secret" => "web_requests#handle_request" # legacy
60 69
 
61
-# To enable DelayedJobWeb, see the 'Enable DelayedJobWeb' section of the README.
62
-#  get "/delayed_job" => DelayedJobWeb, :anchor => false
63
-
64 70
   devise_for :users, :sign_out_via => [ :post, :delete ]
65 71
   get '/auth/:provider/callback', to: 'services#callback'
66 72
 

+ 56 - 45
lib/huginn_scheduler.rb

@@ -1,64 +1,37 @@
1 1
 require 'rufus/scheduler'
2 2
 
3 3
 class HuginnScheduler
4
+  FAILED_JOBS_TO_KEEP = 100
4 5
   attr_accessor :mutex
5 6
 
6 7
   def initialize
7 8
     @rufus_scheduler = Rufus::Scheduler.new
9
+    self.mutex = Mutex.new
8 10
   end
9 11
 
10 12
   def stop
11 13
     @rufus_scheduler.stop
12 14
   end
13 15
 
14
-  def run_schedule(time)
15
-    with_mutex do
16
-      puts "Queuing schedule for #{time}"
17
-      Agent.delay.run_schedule(time)
18
-    end
19
-  end
20
-
21
-  def propagate!
22
-    with_mutex do
23
-      puts "Queuing event propagation"
24
-      Agent.delay.receive!
25
-    end
26
-  end
27
-
28
-  def cleanup_expired_events!
29
-    with_mutex do
30
-      puts "Running event cleanup"
31
-      Event.delay.cleanup_expired!
32
-    end
33
-  end
34
-
35
-  def with_mutex
36
-    ActiveRecord::Base.connection_pool.with_connection do
37
-      mutex.synchronize do
38
-        yield
39
-      end
40
-    end
41
-  end
42
-
43 16
   def run!
44
-    self.mutex = Mutex.new
45
-
46 17
     tzinfo_friendly_timezone = ActiveSupport::TimeZone::MAPPING[ENV['TIMEZONE'].present? ? ENV['TIMEZONE'] : "Pacific Time (US & Canada)"]
47 18
 
48 19
     # Schedule event propagation.
49
-
50 20
     @rufus_scheduler.every '1m' do
51 21
       propagate!
52 22
     end
53 23
 
54 24
     # Schedule event cleanup.
55
-
56 25
     @rufus_scheduler.cron "0 0 * * * " + tzinfo_friendly_timezone do
57 26
       cleanup_expired_events!
58 27
     end
59 28
 
60
-    # Schedule repeating events.
29
+    # Schedule failed job cleanup.
30
+    @rufus_scheduler.every '1h' do
31
+      cleanup_failed_jobs!
32
+    end
61 33
 
34
+    # Schedule repeating events.
62 35
     %w[1m 2m 5m 10m 30m 1h 2h 5h 12h 1d 2d 7d].each do |schedule|
63 36
       @rufus_scheduler.every schedule do
64 37
         run_schedule "every_#{schedule}"
@@ -66,22 +39,60 @@ class HuginnScheduler
66 39
     end
67 40
 
68 41
     # Schedule events for specific times.
69
-
70
-    # Times are assumed to be in PST for now.  Can store a user#timezone later.
71 42
     24.times do |hour|
72 43
       @rufus_scheduler.cron "0 #{hour} * * * " + tzinfo_friendly_timezone do
73
-        if hour == 0
74
-          run_schedule "midnight"
75
-        elsif hour < 12
76
-          run_schedule "#{hour}am"
77
-        elsif hour == 12
78
-          run_schedule "noon"
79
-        else
80
-          run_schedule "#{hour - 12}pm"
81
-        end
44
+        run_schedule hour_to_schedule_name(hour)
82 45
       end
83 46
     end
84 47
 
85 48
     @rufus_scheduler.join
86 49
   end
50
+
51
+  private
52
+  def run_schedule(time)
53
+    with_mutex do
54
+      puts "Queuing schedule for #{time}"
55
+      Agent.delay.run_schedule(time)
56
+    end
57
+  end
58
+
59
+  def propagate!
60
+    with_mutex do
61
+      puts "Queuing event propagation"
62
+      Agent.delay.receive!
63
+    end
64
+  end
65
+
66
+  def cleanup_expired_events!
67
+    with_mutex do
68
+      puts "Running event cleanup"
69
+      Event.delay.cleanup_expired!
70
+    end
71
+  end
72
+
73
+  def cleanup_failed_jobs!
74
+    num_to_keep = (ENV['FAILED_JOBS_TO_KEEP'].presence || FAILED_JOBS_TO_KEEP).to_i
75
+    first_to_delete = Delayed::Job.where.not(failed_at: nil).order("failed_at DESC").offset(num_to_keep).limit(1).pluck(:failed_at).first
76
+    Delayed::Job.where(["failed_at <= ?", first_to_delete]).delete_all if first_to_delete.present?
77
+  end
78
+
79
+  def hour_to_schedule_name(hour)
80
+    if hour == 0
81
+      "midnight"
82
+    elsif hour < 12
83
+      "#{hour}am"
84
+    elsif hour == 12
85
+      "noon"
86
+    else
87
+      "#{hour - 12}pm"
88
+    end
89
+  end
90
+
91
+  def with_mutex
92
+    ActiveRecord::Base.connection_pool.with_connection do
93
+      mutex.synchronize do
94
+        yield
95
+      end
96
+    end
97
+  end
87 98
 end

+ 72 - 0
spec/controllers/jobs_controller_spec.rb

@@ -0,0 +1,72 @@
1
+require 'spec_helper'
2
+
3
+describe JobsController do
4
+
5
+  describe "GET index" do
6
+    before do
7
+      Delayed::Job.create!
8
+      Delayed::Job.create!
9
+      Delayed::Job.count.should > 0
10
+    end
11
+
12
+    it "does not allow normal users" do
13
+      sign_in users(:bob)
14
+      get(:index).should redirect_to(root_path)
15
+    end
16
+    it "returns all jobs", focus: true do
17
+      sign_in users(:jane)
18
+      get :index
19
+      assigns(:jobs).length.should == 2
20
+    end
21
+  end
22
+
23
+  describe "DELETE destroy" do
24
+    before do
25
+      @not_running = Delayed::Job.create
26
+      @running = Delayed::Job.create(locked_at: Time.now, locked_by: 'test')
27
+      sign_in users(:jane)
28
+    end
29
+
30
+    it "destroy a job which is not running" do
31
+      expect { delete :destroy, id: @not_running.id }.to change(Delayed::Job, :count).by(-1)
32
+    end
33
+
34
+    it "does not destroy a running job" do
35
+      expect { delete :destroy, id: @running.id }.to change(Delayed::Job, :count).by(0)
36
+    end
37
+  end
38
+
39
+  describe "PUT run" do
40
+    before do
41
+      @not_running = Delayed::Job.create(run_at: Time.now - 1.hour)
42
+      @running = Delayed::Job.create(locked_at: Time.now, locked_by: 'test')
43
+      @failed = Delayed::Job.create(run_at: Time.now - 1.hour, locked_at: Time.now, failed_at: Time.now)
44
+      sign_in users(:jane)
45
+    end
46
+
47
+    it "queue a job which is not running" do
48
+      expect { put :run, id: @not_running.id }.to change { @not_running.reload.run_at }
49
+    end
50
+
51
+    it "queue a job that failed" do
52
+      expect { put :run, id: @failed.id }.to change { @failed.reload.run_at }
53
+    end
54
+
55
+    it "not queue a running job" do
56
+      expect { put :run, id: @running.id }.not_to change { @not_running.reload.run_at }
57
+    end
58
+  end
59
+
60
+  describe "DELETE destroy_failed" do
61
+    before do
62
+      @failed = Delayed::Job.create(failed_at: Time.now - 1.minute)
63
+      @running = Delayed::Job.create(locked_at: Time.now, locked_by: 'test')
64
+      sign_in users(:jane)
65
+    end
66
+
67
+    it "just destroy failed jobs" do
68
+      expect { delete :destroy_failed, id: @failed.id }.to change(Delayed::Job, :count).by(-1)
69
+      expect { delete :destroy_failed, id: @running.id }.to change(Delayed::Job, :count).by(0)
70
+    end
71
+  end
72
+end

+ 1 - 0
spec/env.test

@@ -3,3 +3,4 @@ TWITTER_OAUTH_KEY=twitteroauthkey
3 3
 TWITTER_OAUTH_SECRET=twitteroauthsecret
4 4
 THIRTY_SEVEN_SIGNALS_OAUTH_KEY=TESTKEY
5 5
 THIRTY_SEVEN_SIGNALS_OAUTH_SECRET=TESTSECRET
6
+FAILED_JOBS_TO_KEEP=2

+ 2 - 1
spec/fixtures/users.yml

@@ -10,4 +10,5 @@ jane:
10 10
   email: "jane@example.com"
11 11
   username: jane
12 12
   invitation_code: <%= User::INVITATION_CODES.last %>
13
-  scenario_count: 1
13
+  scenario_count: 1
14
+  admin: true

+ 32 - 0
spec/helpers/jobs_helper_spec.rb

@@ -0,0 +1,32 @@
1
+require 'spec_helper'
2
+
3
+describe JobsHelper do
4
+  let(:job) { Delayed::Job.new }
5
+
6
+  describe '#status' do
7
+    it "works for failed jobs" do
8
+      job.failed_at = Time.now
9
+      status(job).should == '<span class="label label-danger">failed</span>'
10
+    end
11
+
12
+    it "works for running jobs" do
13
+      job.locked_at = Time.now
14
+      job.locked_by = 'test'
15
+      status(job).should == '<span class="label label-info">running</span>'
16
+    end
17
+
18
+    it "works for queued jobs" do
19
+      status(job).should == '<span class="label label-warning">queued</span>'
20
+    end
21
+  end
22
+
23
+  describe '#relative_distance_of_time_in_words' do
24
+    it "in the past" do
25
+      relative_distance_of_time_in_words(Time.now-5.minutes).should == '5m ago'
26
+    end
27
+
28
+    it "in the future" do
29
+      relative_distance_of_time_in_words(Time.now+5.minutes).should == 'in 5m'
30
+    end
31
+  end
32
+end

+ 77 - 0
spec/lib/huginn_scheduler_spec.rb

@@ -0,0 +1,77 @@
1
+require 'spec_helper'
2
+
3
+describe HuginnScheduler do
4
+  before(:each) do
5
+    @scheduler = HuginnScheduler.new
6
+    stub
7
+  end
8
+
9
+  it "should stop the scheduler" do
10
+    mock.instance_of(Rufus::Scheduler).stop
11
+    @scheduler.stop
12
+  end
13
+
14
+  it "schould register the schedules with the rufus scheduler and run" do
15
+    mock.instance_of(Rufus::Scheduler).join
16
+    @scheduler.run!
17
+  end
18
+
19
+  it "should run scheduled agents" do
20
+    mock(Agent).run_schedule('every_1h')
21
+    mock.instance_of(IO).puts('Queuing schedule for every_1h')
22
+    @scheduler.send(:run_schedule, 'every_1h')
23
+  end
24
+
25
+  it "should propagate events" do
26
+    mock(Agent).receive!
27
+    stub.instance_of(IO).puts
28
+    @scheduler.send(:propagate!)
29
+  end
30
+
31
+  it "schould clean up expired events" do
32
+    mock(Event).cleanup_expired!
33
+    stub.instance_of(IO).puts
34
+    @scheduler.send(:cleanup_expired_events!)
35
+  end
36
+
37
+  describe "#hour_to_schedule_name" do
38
+    it "for 0h" do
39
+      @scheduler.send(:hour_to_schedule_name, 0).should == 'midnight'
40
+    end
41
+
42
+    it "for the forenoon" do
43
+      @scheduler.send(:hour_to_schedule_name, 6).should == '6am'
44
+    end
45
+
46
+    it "for 12h" do
47
+      @scheduler.send(:hour_to_schedule_name, 12).should == 'noon'
48
+    end
49
+
50
+    it "for the afternoon" do
51
+      @scheduler.send(:hour_to_schedule_name, 17).should == '5pm'
52
+    end
53
+  end
54
+
55
+  describe "cleanup_failed_jobs!" do
56
+    before do
57
+      3.times do |i|
58
+        Delayed::Job.create(failed_at: Time.now - i.minutes)
59
+      end
60
+      @keep = Delayed::Job.order(:failed_at)[1]
61
+    end
62
+
63
+    it "work with set FAILED_JOBS_TO_KEEP env variable", focus: true do
64
+      expect { @scheduler.send(:cleanup_failed_jobs!) }.to change(Delayed::Job, :count).by(-1)
65
+      expect { @scheduler.send(:cleanup_failed_jobs!) }.to change(Delayed::Job, :count).by(0)
66
+      @keep.id.should == Delayed::Job.order(:failed_at)[0].id
67
+    end
68
+
69
+
70
+    it "work without the FAILED_JOBS_TO_KEEP env variable" do
71
+      old = ENV['FAILED_JOBS_TO_KEEP']
72
+      ENV['FAILED_JOBS_TO_KEEP'] = nil
73
+      expect { @scheduler.send(:cleanup_failed_jobs!) }.to change(Delayed::Job, :count).by(0)
74
+      ENV['FAILED_JOBS_TO_KEEP'] = old
75
+    end
76
+  end
77
+end